const config = require('./config.js')

class Api {
    constructor() {
        this.baseUrl = config.baseUrl
    }

    // 统一请求方法 - 严格对应后端header
    request(url, method = 'GET', data = {}) {
        return new Promise((resolve, reject) => {
            const userId = wx.getStorageSync('userId') || 1 // 测试用默认用户ID

            wx.request({
                url: this.baseUrl + url,
                method: method,
                data: data,
                header: {
                    'Content-Type': 'application/json',
                    'X-User-ID': userId // 严格对应后端要求的header
                },
                timeout: config.timeout,
                success: (res) => {
                    if (res.statusCode === 200) {
                        resolve(res.data)
                    } else {
                        reject(res.data)
                    }
                },
                fail: (err) => {
                    reject(err)
                }
            })
        })
    }

    // 帖子相关API - 严格对应后端接口
    post = {
        // 获取帖子列表 - GET /posts
        getPosts: (page = 1, size = 10, category = '') => {
            let url = `/posts?page=${page}&size=${size}`
            if (category) {
                url += `&category=${category}`
            }
            return this.request(url)
        },

        // 获取帖子详情 - GET /posts/{id}
        getPostDetail: (id) => {
            return this.request(`/posts/${id}`)
        },

        // 创建帖子 - POST /posts
        createPost: (data) => {
            return this.request('/posts', 'POST', data)
        },

        // 更新帖子 - PUT /posts/{id}
        updatePost: (id, data) => {
            return this.request(`/posts/${id}`, 'PUT', data)
        },

        // 删除帖子 - DELETE /posts/{id}
        deletePost: (id) => {
            return this.request(`/posts/${id}`, 'DELETE')
        },

        // 点赞帖子 - POST /posts/{id}/like
        likePost: (id) => {
            return this.request(`/posts/${id}/like`, 'POST')
        },

        // 取消点赞 - POST /posts/{id}/unlike
        unlikePost: (id) => {
            return this.request(`/posts/${id}/unlike`, 'POST')
        },

        // 获取用户帖子 - GET /posts/my
        getUserPosts: () => {
            return this.request('/posts/my')
        },

        // 获取用户点赞帖子 - GET /posts/my/likes
        getUserLikedPosts: () => {
            return this.request('/posts/my/likes')
        },

        // 获取推荐帖子 - GET /posts/recommended
        getRecommendedPosts: () => {
            return this.request('/posts/recommended')
        }
    }

    // 团队相关API - 严格对应后端接口
    team = {
        // 申请加入团队 - POST /team/apply
        applyToTeam: (data) => {
            return this.request('/team/apply', 'POST', data)
        },

        // 获取团队详情 - GET /team/{postId}/detail
        getTeamDetail: (postId) => {
            return this.request(`/team/${postId}/detail`)
        },

        // 检查点赞状态 - GET /team/check-like
        checkLikeStatus: (postId) => {
            return this.request(`/team/check-like?postId=${postId}`)
        }
    }
}

module.exports = new Api()